package org.eerbaar.parser.cky;

import java.util.ArrayList;
import java.util.List;

import org.eerbaar.parser.core.def.Symbol;
import org.eerbaar.parser.core.exe.HasSymbol;
import org.eerbaar.parser.core.exe.SymbolInstance;

class Level
    implements HasSymbol {

    final int index;
    final List<SymbolArray> array = new ArrayList<SymbolArray>();

    public Level(SymbolInstance[] input, int index) {
        this.index = index;
        for (SymbolInstance symbol : input) {
            SymbolArray s = new SymbolArray(symbol);
            array.add(s);
        }
    }

    public int getIndex() {
        return this.index;
    }

    public SymbolArray getSymbolArray(int i) {
        return array.get(i - 1);
    }

    public SymbolArray getSymbolArray(Symbol symbol) {
        SymbolArray result = null;
        for (SymbolArray s : array) {
            if (s.getInputSymbol().equals(symbol)) {
                result = s;
                break;
            }
        }
        return result;
    }

    @Override
    public SymbolInstance next() {
        SymbolInstance result = null;
        for (SymbolArray s : array) {
            result = s.next();
            if (result != null) {
                break;
            }
        }
        return result;
    }

    @Override
    public void remove(SymbolInstance symbol) {
        for (SymbolArray s : array) {
            s.remove(symbol);
        }
    }

    @Override
    public String toString() {
        String s = "";
        List<String> ls = new ArrayList<String>();
        for (SymbolArray ss : array) {
            ls.add(new Util().pad(ss.toString()));
        }
        s += ls.toString() + "\n";
        return s;
    }
}
